package noahzu.github.io.baselib.utils.fragmentMaker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

/**
 * Created by gejinpeng on 2017/9/28.
 */

public final class FragmentSpecCreator {
    private FragmentMaker transfer;
    private FragmentSpec mSpec;

    FragmentSpecCreator(FragmentMaker transfer) {
        this.transfer = transfer;
        this.mSpec = FragmentSpec.getCleanInstance();
    }

    public FragmentSpecCreator fragment(Class fragment) {
        this.mSpec.fragmentClass = fragment;
        return this;
    }

    public FragmentSpecCreator fragment(Fragment fragment) {
        this.mSpec.fragment = fragment;
        return this;
    }

    public FragmentSpecCreator arguments(Bundle arguments) {
        this.mSpec.arguments = arguments;
        return this;
    }

    public FragmentSpecCreator isShowTitleView(boolean isShowTitleView){
        this.mSpec.mIsShowTitleView = isShowTitleView;
        return this;
    }

    public FragmentSpecCreator isTitleViewBackFinish(boolean isTitleViewBackFinish){
        this.mSpec.mIsTitleViewBackFinish = isTitleViewBackFinish;
        return this;
    }

    public FragmentSpecCreator titleViewText(String title){
        this.mSpec.mTitle = title;
        return this;
    }

    public void start() {
        Activity activity = this.transfer.getActivity();
        if(activity != null) {
            Intent intent = new Intent(activity, FragmentTransferActivity.class);
            Fragment fragment = this.transfer.getFragment();
            if(fragment != null) {
                fragment.startActivity(intent);
            } else {
                activity.startActivity(intent);
            }
        }
    }

    public void startForResult(int requestCode) {
        Activity activity = this.transfer.getActivity();
        if(activity != null) {
            Intent intent = new Intent(activity, FragmentTransferActivity.class);
            Fragment fragment = this.transfer.getFragment();
            if(fragment != null) {
                fragment.startActivityForResult(intent, requestCode);
            } else {
                activity.startActivityForResult(intent, requestCode);
            }
        }
    }

}
